package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2024/9/11 10:11
 * @apiNote
 */

public class Test02555MaximizeWin {
    public static void main(String[] args) {
        int[] prizePositions = {1, 1,2,2,3,3,5};
        int k = 2;
        System.out.println(maximizeWin(prizePositions, k));
    }
    public static int maximizeWin(int[] prizePositions, int k) {
        int n = prizePositions.length;
        int[] dp = new int[n+1];
        int ans = 0;
        for (int left = 0, right = 0; right < n; right++) {
            while(prizePositions[right] - prizePositions[left] > k){
                left++;
            }
            ans = Math.max(ans,right-left+1+dp[left]);
            dp[right+1] = Math.max(dp[right],right - left +1);
        }
        return ans;
    }
}
